package com.umeng.kaoti5.model;

import com.example.mvplibrary.mvplibrary.model.BaseModel;
import com.example.mylibrary.client.HttpClient;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.trello.rxlifecycle2.LifecycleProvider;
import com.umeng.kaoti5.callback.HttpCallBack;
import com.umeng.kaoti5.data.HighOpinion;
import com.umeng.kaoti5.presenter.MainPresenter;

import java.util.HashMap;
import java.util.List;

public class MainModel implements BaseModel {

    public void getData(final MainPresenter mainPresenter, LifecycleProvider lifecycleProvider) {

        HashMap<String, Object> map = new HashMap<>();
        map.put("cityid",1);
        map.put("operaVersion","5.1.1");

        new HttpClient.Builder()
                .setBaseUrl("https://api.zhuge.com/home/v1/Appindex/")
                .setApiUrl("showHighOpinion")
                .setParamser(map)
                .setLifecycleProvider(lifecycleProvider)
                .post()
                .build()
                .excute(new HttpCallBack<List<HighOpinion.ListBean>>() {


                    @Override
                    protected void onSucess(List<HighOpinion.ListBean> listBeans) {
                        mainPresenter.OnSuccess(listBeans);
                    }

                    @Override
                    public List<HighOpinion.ListBean> convert(JsonElement jsonElement) {
                        HighOpinion highOpinion = new Gson().fromJson(jsonElement, HighOpinion.class);

                        return highOpinion.getList();
                    }

                    @Override
                    protected void error(String error, int code) {
                        mainPresenter.OnError(error,code);
                    }
                });
    }
}
